package tgc.edu.ljc.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.custom.JqGridParam;
import tgc.edu.ljc.custom.JqGridUtils;
import tgc.edu.ljc.entity.Course;
import tgc.edu.ljc.entity.Grade;
import tgc.edu.ljc.entity.Stu;
import tgc.edu.ljc.entity.SysUser;
import tgc.edu.ljc.entity.TbClass;
import tgc.edu.ljc.security.UserUtils;
import tgc.edu.ljc.service.ClassService;
import tgc.edu.ljc.service.CourseService;
import tgc.edu.ljc.service.GradeService;
import tgc.edu.ljc.service.StudentService;

@Controller
@RequestMapping(value="/grade")
public class GradeController {
	
	@Autowired
	private GradeService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private UserUtils UserUtils;
	@Autowired
	private StudentService stuService;
	
	@RequestMapping(value="/classGrade")
	public String classGrade(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		List<Course> courses = courseService.findAll();
		map.put("classes", classes);
		map.put("courses", courses);
		return"grade/classGrade";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(Integer classId,Integer courseId,JqGridParam param) {
		Page<Grade> page;
		Pageable pageable = param.buildPageable();
		if(classId==0) {
			page=service.findByIdIsNull(pageable);
		}else {
			page=service.findByClassIdAndCourseId(classId,courseId,pageable);
		}
		return JqGridUtils.getResult(page);
	}
	
	@RequestMapping(value="/stuList")
	@ResponseBody
	public Object stuList(Integer stuId,JqGridParam param) {
		Page<Grade> page;
		Pageable pageable = param.buildPageable();
		if(stuId==0) {
			page=service.findByIdIsNull(pageable);
		}else {
			page=service.findByStuId(stuId,pageable);
		}
		return JqGridUtils.getResult(page);
	}
	
	@RequestMapping(value="/celledit")
	@ResponseBody
	public Object celledit(Integer id,Integer grade) {
		Grade model=service.findById(id);
		model.setGrade(grade);
		service.save(model);
		return "Ok";
	}
	
	@RequestMapping(value="/studentGrade")
	public String studentGrade(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		map.put("classes", classes);
		return "grade/studentGrade";
	}
	
	@RequestMapping(value="/peasonGrade")
	public String peasonGrade(ModelMap  map) {
		Integer id = UserUtils.getUser().getId();
		List<Grade> grades=service.findByStuId(id);
		map.put("grades", grades);
		return "grade/peasonGrade";
	}
	
	@RequestMapping(value="/findGradeByStudentId")
	@ResponseBody
	public Object findGradeByStudentId(Integer studentId) {
		List<Grade> list=service.findByStuId(studentId);
		return list;
	}
	
	@RequestMapping(value="/allStudentGrade")
	public String allStudentGrade(ModelMap map) {
		SysUser user = UserUtils.getUser();
		Stu stu=(Stu) user;
		Integer classId = stu.getTbClass().getId();
		List<Course> courses= service.findCoursesByClassId(classId);
		map.put("courses", courses);
		List<HashMap<String, Object>> grades=new ArrayList<>();
		List<Stu> students=stuService.findByTbClassId(classId);
		for (Stu stu2 : students) {
			HashMap<String, Object> o=new HashMap<>();
			o.put("stuNum", stu2.getUsername());
			o.put("name",stu2.getName());
			List<Integer> datas=new ArrayList<>();
			for (Course course : courses) {
				Grade g=service.findByStuIdAndCourseId(stu2.getId(),course.getId());
				datas.add(g==null?null:g.getGrade());
			}
			o.put("datas",datas);
			grades.add(o);
		}
		map.put("grades", grades);
		return "grade/allStudentGrade";
	}
}
